﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicClass;
using Xinkui.WebModel.Cache;
using CommonFramework.Utility;
using Xinkui.DataAccess.User;
using Xinkui.Entity.AutoGenerator;
using Xinkui.WebModel;
using Xinkui.WebModel.Helper;

public partial class registerstep1 : PageBase {
    protected override void BindDataSource() {
        base.BindDataSource();

        rptLocation.DataSource = CountryCache.List().Values;
        rptLocation.DataBind();

        if (!this.IsPostBack) {
            if (Session["RUserInfo"] != null) {
                Session.Remove("RUserInfo");
            }
            if (Session["RCompanyInfo"] != null) {
                Session.Remove("RCompanyInfo");
            }
        }

    }

    /// <summary>
    /// 表单提交数据处理
    /// </summary>
    protected override void HandlePostActions() {
        base.HandlePostActions();


        string username = WebHelper.GetFormValue<string>("txtUserName", string.Empty);

        int location = WebHelper.GetFormValue<int>("dropBussinessLoc", -1);
        int meberType = WebHelper.GetFormValue<int>("rbtnMemberType", -1);
        string email = WebHelper.GetFormValue<string>("txtEmail", string.Empty);
        string pwd = WebHelper.GetFormValue<string>("txtPwd", string.Empty);
        string firstname = WebHelper.GetFormValue<string>("txtFirstName", string.Empty);
        string lastname = WebHelper.GetFormValue<string>("txtLastName", string.Empty);
        string companyName = WebHelper.GetFormValue<string>("txtCompanyName", string.Empty);
        string cnCompanyName = WebHelper.GetFormValue<string>("txtCNCompany", string.Empty);
        string[] tels = new string[3];
        tels[0] = WebHelper.GetFormValue<string>("txtTel1", string.Empty);
        tels[1] = WebHelper.GetFormValue<string>("txtTel2", string.Empty);
        tels[2] = WebHelper.GetFormValue<string>("txtTel3", string.Empty);
        string vCode = WebHelper.GetFormValue<string>("txtCode", string.Empty);
        string code = Session["SK_SecrityCode"] as string;
        if (!vCode.Equals(code)) { UIHelper.ShowErrorTips("Verification Code Not Matched"); return; }

        UserInfoMaintain_Entity userInfo = new UserInfoMaintain_Entity();
        CompanyInfoMaintain_Entity companyInfo = new CompanyInfoMaintain_Entity();

        userInfo.UserName = username;
        userInfo.Password = pwd;
        userInfo.CompanyID = 0;
        userInfo.Email = email;
        userInfo.FirstName = firstname;
        userInfo.LastName = lastname;
        userInfo.PhoneNum = string.Join("-", tels);
        userInfo.UpdateDate = DateTime.Now;
        companyInfo.CnCompanyName = cnCompanyName;
        companyInfo.CompanyName = companyName;
        companyInfo.PhoneNum = userInfo.PhoneNum;
        companyInfo.AuditStatus = (short)AuditStatusEnum.New;
        companyInfo.MemberType = (short)meberType;
        companyInfo.UpdateDate = DateTime.Now;
        companyInfo.CreateDate = DateTime.Now;
        companyInfo.UpdateIp = WebHelper.UserIP;
        companyInfo.CreateIp = WebHelper.UserIP;

        companyInfo.CompanyEmail = email;
        if (UserDataAccess.Register(userInfo, companyInfo)) {
            Session["RUserInfo"] = userInfo;
            Session["RCompanyInfo"] = companyInfo;
            Response.Redirect("registerstep2.aspx");
        }
        else {
            UIHelper.ShowErrorTips("Register Error");
        }

    }
}